package com.kevineric.pokedex;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
 * 
 * @author echeek
 *	Creates sqlite tables from parsed pokedex data
 */
public class PokedexDbAdapter {

	private static final String DATABASE_NAME="pokedex";
	private static final int DATABASE_VERSION=1;//TODO: read this from file
	
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    private final Context mCtx;
    
    private static final String[] DB_TABLES={ "pokemon", "pokemonTypes", "pokemonAbilities", "pokemonEvolutions", "pokemonAttacks", "pokemonLocations",
    		"pokemonFlavorText", "pokemonEggGroups", "typeDamages"
    };
    
    private static final String[] DB_CREATE_TABLES={
    	//pokemon table
    	"CREATE TABLE pokemon (pokemonID INTEGER PRIMARY KEY, name TEXT NOT NULL, genderRatio REAL, classification TEXT, height TEXT, weight TEXT, captureRate REAL, "+
    		"baseEggSteps TEXT, experienceGrowth TEXT, baseHappiness TEXT, effortValuesEarned TEXT, colour TEXT, fleeRate TEXT, "+
    		"baseHP INTEGER, baseAttack INTEGER, baseDefense INTEGER, baseSpAttack INTEGER, baseSpDefense INTEGER, baseSpeed INTEGER)",
    		
    	//pokemon type table
    	"CREATE TABLE pokemonTypes (pokemonID INTEGER, type TEXT)",
    	//pokemon abilities table
    	"CREATE TABLE pokemonAbilities (pokemonID INTEGER, ability TEXT)",
    	//evolution table
    	"CREATE TABLE pokemonEvolutions (currentPokemonID INTEGER, successorID INTEGER, evolveMethod TEXT)",
    	//pokemon -> learnable attacks
    	"CREATE TABLE pokemonAttacks (pokemonID INTEGER, attackName TEXT, leanAtLevel INTEGER)",
    	//Damage table
    	"CREATE TABLE typeDamages (attackType TEXT, defendType TEXT, damageMultiplier REAL)",
    	//Pokemon flavor text
    	"CREATE TABLE pokemonFlavorText (pokemonID INTEGER, gameName TEXT, flavorText TEXT)",
    	"CREATE TABLE pokemonLocations (pokemonID INTEGER, gameName TEXT, locationText TEXT)",
    	"CREATE TABLE pokemonEggGroups (pokemonID INTEGER, eggGroup TEXT)"
    	
    	
    	//attackdex table
    	
    };
	
	private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        	System.out.println("Attempting to create DB tables");
        	//db creation code
        	for( int i=0; i<DB_CREATE_TABLES.length; i++ ){
        		
        		db.execSQL(DB_CREATE_TABLES[i]);
        	}
        	System.out.println("Created DB tables");
        	
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            System.out.println( "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            for( int i=0; i<DB_TABLES.length; i++){
            	db.execSQL("DROP TABLE IF EXISTS "+DB_TABLES[i]);
            }
            
            onCreate(db);
        }
    }
	
    public PokedexDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }
	
    
    public PokedexDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        mDbHelper.close();
    }
	
    public Cursor fetchPokemon(String name) throws SQLException {
    	Cursor mCursor=mDb.query(true, "pokemon", new String[]{}, "name ="+name, null, null, null, null, null);
    	
    	if( mCursor!=null){
    		mCursor.moveToFirst();
    	}
    	return mCursor;
    }
    
    
    public void testDB() throws SQLException {
    	String testInsert="INSERT INTO pokemonTypes (pokemonID, type) VALUES (49, "
    	
    	
    }
    
    
	
}
